#include <QDebug>
#include <QMessageBox>
#include <QDir>
#include "configcapture.h"
#include "ui_configcapture.h"
#include "ewha.h"
#include "SettingHelper.h"

ConfigCapture::ConfigCapture(QWidget *parent) :
	QWidget(parent),
	ui(new Ui::ConfigCapture)
{
	ui->setupUi(this);

	connect(ui->controlPanel, SIGNAL(SignalJustCaptured(QString)), SIGNAL(SignalJustCaptured(QString)));
	connect(ui->fullTeethPanel, SIGNAL(SignalSelectedTeeth(int)), SIGNAL(SignalSelectedTeeth(int)));
	connect(ui->fullTeethPanel, SIGNAL(SignalSelectedTeeth(int)), SLOT(SlotTeethSelected(int)));
	connect(ui->controlPanel, SIGNAL(SignalExitCapture()), SIGNAL(SignalProgressEnd()));
	connect(ui->controlPanel, SIGNAL(SignalExitCapture()), SIGNAL(SignalExitCapture()));
	connect(ui->controlPanel, SIGNAL(SignalStandby(int,int,int)), SLOT(SlotStandby(int,int,int)));
	connect(ui->controlPanel, SIGNAL(SignalReady()), SLOT(SlotReady()));
}

ConfigCapture::~ConfigCapture()
{
	delete ui;
}

void ConfigCapture::reset()
{
	ui->controlPanel->reset();
}

void ConfigCapture::SlotStandbyDone()
{
	ui->controlPanel->setLamp(0, eStGreen);
	ui->controlPanel->setLamp(1, eStGray);
	ui->controlPanel->setLamp(2, eStGray);
	ui->controlPanel->setLamp(3, eStGray);
	emit SignalMessage(QString());
	emit SignalProgressEnd();
}

void ConfigCapture::SlotReadyDone()
{
	ui->controlPanel->setLamp(0, eStGray);
	ui->controlPanel->setLamp(1, eStGreen);
	ui->controlPanel->setLamp(2, eStGray);
	ui->controlPanel->setLamp(3, eStGray);
	emit SignalMessage(QString());
	emit SignalProgressEnd();
}

void ConfigCapture::SlotEXPOON()
{
	ui->controlPanel->setLamp(1, eStGray);
	ui->controlPanel->setLamp(2, eStOrange);
	ui->controlPanel->setLamp(0, eStGray);
	ui->controlPanel->setLamp(3, eStGray);
}

void ConfigCapture::SlotEXPOOFF()
{
	ui->controlPanel->setLamp(2, eStGray);
	ui->controlPanel->setLamp(3, eStBlue);
	ui->controlPanel->setLamp(0, eStGray);
	ui->controlPanel->setLamp(1, eStGray);
	emit SignalProgressStart();
}

void ConfigCapture::SlotIMGDone()
{
	emit SignalProgressEnd();

	QString exePath;
	SettingHelper::loadConsole(&exePath);
	qDebug() << "[load exepath]" << exePath;
	QDir dir(QFileInfo(exePath).dir().
			 filePath(SettingHelper::loadMode() == 0 ? strBinningImgPath : strNormalImgPath));
	qDebug() << dir;
	QFileInfoList list = dir.
			entryInfoList(QStringList() << "*.raw", QDir::Files, QDir::Time | QDir::Reversed);
	qDebug() << list.count();

	if(list.count() > 0)
	{
		emit SignalJustCaptured(list[0].absoluteFilePath());
	}
	else
	{
		ui->controlPanel->reset();
		QMessageBox::warning(this,
							 "생성된 이미지 없음",
							 dir.absolutePath());
	}
}

void ConfigCapture::SlotError()
{
	ui->controlPanel->reset();
	QMessageBox::warning(this, "EWHA",
							 "에러 발생");
	emit SignalProgressEnd();

}

void ConfigCapture::SlotCaptureStart()
{
	QMessageBox::information(this, "EWHA",
							 "치아를 선택하세요");
}

void ConfigCapture::SlotTeethSelected(int iTeeth)
{
	QString strMsg("%1 번 치아를 촬영합니다");
	emit SignalMessage(strMsg.arg(iTeeth));
	QMessageBox::information(this, "EWHA",
							 "수진자를 정렬하세요");
	QMessageBox::information(this, "EWHA",
							 "촬영을 시작합니다\n관전류, 조사시간 선택하세요");
	emit SignalStartConsole();
	QMessageBox::information(this, "EWHA",
							 "스텐바이를 누르세요");
}

void ConfigCapture::SlotStandby(int iVoltage, int iCurrent, int iTime)
{
	QString strMsg("촬영 준비중(Standby)");
	emit SignalControlValue(iVoltage, iCurrent, iTime);
	emit SignalMessage(strMsg);
	emit SignalProgressStart();
	emit SignalSendStandby();
}

void ConfigCapture::SlotReady()
{
	QString strMsg("촬영 준비중(Ready)");
	emit SignalMessage(strMsg);
	emit SignalProgressStart();
	emit SignalSendReady();
}
